/**
 * UI全局二次封装方法.
 */
// import $_fn from '~plugins'
class ui {
    constructor($rootScope,$location,$timeout,$interval,$uibModal) {
        this.$rootScope = $rootScope,
        this.$location = $location,
        this.$timeout  = $timeout,
        this.$interval = $interval,
        this.$uibModal = $uibModal
    }
    /**
     * 弹窗二次封装
     * @param options
     * (string)content   弹窗内容、可传directive
     * (string)size      弹窗大小
     * (string)title     弹窗标题
     * (function)ok      提交成功回调
     */
    modal (options) {
        var _this = this
        //如果传入的是指令
        if(options.content.indexOf('></') > 0){
            //返回了一个全局$uibModal实例用于处理自定义弹窗关闭
            _this.$rootScope.$uiDirectiveModal = _this.$uibModal.open({
                backdrop: 'static', //点击空白处不关闭.
                keyboard: false,    //esc键盘不关闭.
                template: options.content
            })
            return
        }else{
            //正常的初始弹窗
            var $uibModalInstance =_this.$uibModal.open({
                templateUrl: '/include/modal.html',
                size: options.size,
                controller: function($scope) {
                    $scope.title = options.title;
                    $scope.content = options.content
                    $scope.cancel = function(){
                        $uibModalInstance.close();
                    }
                    $scope.ok =function () {
                        options.ok && options.ok($uibModalInstance)
                    }
                }
            });
        }
    }
    /**
     * toast弹窗
     * @param options
     * (string)content        提示内容
     * (string)position       位置
     * (string)type           提示类型(warning,question,success,info)
     * (function)timeout      消失时间
     */
    toast (options) {
        var toastEl
        var objSample = {
            text:options.text,
            position:options.position ||'center',
            type:options.type || 'success',
            appendTo:options.appendTo || '.position-view',
            timeout: options.timeout || 3000
        }
        toastEl = angular.element('<div class="toast toast-'+objSample.position+' toast-'+objSample.type+'"><span class="toast-type icon icon-'+objSample.type+'-on"></span><span class="toast-text">'+objSample.text+'</span></div>');
        if (options.text != undefined && options.text != "") {
            $(objSample.appendTo).append(toastEl)
        }
        $('.toast').on('click',function () {
            toastEl.remove()
        })
        setTimeout(function () {
            toastEl.remove();
        }, objSample.timeout);
    }
}
ui.$injecter = ['$rootScope','$location','$timeout','$interval','$uibModal']
export default angular.module('app.ui', [])
    .service('$ui', ui)
    .name
